import {
  captchaImage,
  getcode,
  wxlogin,
  login
} from "../../api/index.js"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    img_code: '',
    code: '',
    agree_status: 0,
    checked: false,
    show1: false,
    show2: false,
    uuid: null,
    img: null,
    TIME_COUNT: 120, // 验证码倒计时60s
    count: '',
    issendcode: true,
    timer: null,
  },
  getImg() {
    captchaImage().then(res => {
      console.log(res)
      res.img = 'url(data:image/gif;base64,' + res.img + ')'
      this.setData({
        uuid: res.uuid,
        img: res.img
      })
    })
  },
  getCode() {
    var _this = this;
    // else if (this.data.img_code == '') {
    //   wx.showToast({
    //     title: '请填写图形验证码',
    //     icon: 'none'
    //   })
    // }
    if (this.data.phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
    }  else {
      getcode(this.data.phone).then(res => {
        if (res.code == 200) {
          if (!_this.data.timer) {
            // const phoneNumber = String(phone)
            _this.setData({
              count: _this.data.TIME_COUNT,
              issendcode: false
            })
            _this.data.timer = setInterval(() => {
              if (_this.data.count > 0 && _this.data.count <= _this.data.TIME_COUNT) {
                _this.setData({
                  count: _this.data.count - 1
                })
              } else {
                clearInterval(_this.data.timer)
                _this.data.timer = null
                _this.setData({
                  issendcode: true
                })
              }
            }, 1000)
          }
          wx.showToast({
            title: '验证码已发送至您的手机，请注意查收~',
            icon: 'none'
          })
        } else if (res.code == 500) {
          wx.showToast({
            title: res.msg,
            icon: "none"
          })
        }
      })

    }
  },
  phonelogin() {
    var _this = this;
    if (this.data.phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
    } else if (this.data.code == '') {
      wx.showToast({
        title: '请填写短信验证码',
        icon: 'none'
      })
    } else if (this.data.checked == false) {
      wx.showToast({
        title: '请阅读并同意协议',
        icon: 'none'
      })
    } else {
      var data = {
        phone: this.data.phone,
        code: this.data.code
      }
      login(data).then(res => {
        if(res.code != 200){
          wx.showToast({
            title: res.msg,
            icon: 'none',
          })
          return
        }
        wx.setStorageSync('token', res.token)
        wx.setStorageSync('userInfo', res.user)
        wx.setStorageSync('accessToken', res.tokens.accessToken)
        wx.setStorageSync('refreshToken', res.tokens.refreshToken)
        app.globalData.token = res.token
        app.globalData.userInfo = res.user
        wx.showToast({
          title: '登陆成功！',
          icon: 'none',
          success: function () {
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/home/home',
              })
            }, 2000);
          }
        })
      })
    }
  },
  quicklogin() {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onChoiceChange(event) {
    this.setData({
      checked: !this.data.checked,
    });
  },
  phoneChange(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  deletephone() {
    this.setData({
      phone: null
    })
  },
  imgChange(e) {
    this.setData({
      img_code: e.detail.value
    })
  },
  codeChange(e) {
    this.setData({
      code: e.detail.value
    })
  },
  useAgree(e) {
    var checked = e.detail.checked
    this.setData({
      checked: checked
    })
  },
  wxClick() {
    // wx.reLaunch({
    //   url: '/pages/home/home',
    // })
  },
  clickXieyi() {
    this.setData({
      show1: true
    })
  },

  clickYinsi() {
    this.setData({
      show2: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getImg()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})